package top.kuxiao.crmserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.kuxiao.crmserver.pojo.AccountManager;
import top.kuxiao.crmserver.pojo.IUser;
import top.kuxiao.crmserver.pojo.SalesExecutive;
import top.kuxiao.crmserver.pojo.SysUser;
import top.kuxiao.crmserver.service.IAccountManagerService;
import top.kuxiao.crmserver.service.ISalesExecutiveService;
import top.kuxiao.crmserver.service.ISysUserService;
import top.kuxiao.crmserver.utils.CookieUtil;
import top.kuxiao.crmserver.utils.JWTUtil;
import top.kuxiao.crmserver.utils.ResultUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 登录控制
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-13
 */
@Api(tags ="登录系统")
@CrossOrigin
@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    IAccountManagerService iAccountManagerService;

    @Autowired
    ISalesExecutiveService iSalesExecutiveService;

    @Autowired
    JWTUtil jwtUtil;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    CookieUtil cookieUtil;

    @ApiOperation(value = "登录的账号保存")
    @RequestMapping(value = "toLogin", method = RequestMethod.POST)
    public ResultUtil toLogin(HttpServletRequest request) {

        if (cookieUtil.buttonFlag(request)) {
            IUser object = cookieUtil.getObject(request);

            if (object != null) {
                return ResultUtil.resObj(object);
            }

        }

        return ResultUtil.resMsgError("");

    }



    /**
     * 登录
     */
    @ApiOperation(value = "登录验证及保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "账号"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "flag", value = "保存登录信息的按钮")
    })
    @RequestMapping(value = "loginIn", method = RequestMethod.POST)
    public ResultUtil login(
            String name,
            String password,
            @RequestParam(defaultValue = "false") Boolean flag,
            HttpServletResponse response) {


        HashMap<String, Object> map = new HashMap<>();

        IUser one = null;

        //在用户表中查找权限
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("name", name);
        SysUser sysUser = sysUserService.getOne(sysUserQueryWrapper);

        String role;



        if (sysUser!=null){
            role=sysUser.getPower();

            if ("AM".equals(role)) {
                QueryWrapper<AccountManager> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("am_name", name).eq("am_password", password);
                one = iAccountManagerService.getOne(queryWrapper);

            } else if ("SE".equals(role)) {
                QueryWrapper<SalesExecutive> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("se_name", name).eq("se_password", password);
                one = iSalesExecutiveService.getOne(queryWrapper);

            }

        }else {
            return ResultUtil.resMsgError("没有该用户");
        }


        //存放登录者的身份
        map.put("role", role);
        map.put("flag", flag);
        map.put("data",sysUser);

        String token = jwtUtil.createToken(map);
        Cookie cookie = new Cookie("user", token);
        cookie.setPath("/");
        if (one != null) {
            if ("禁用".equals(sysUser.getState())) {
                return ResultUtil.resMsgError("该用户已经被禁用");
            }

            cookie.setMaxAge(24 * 60 * 60 * 60);

        } else {
            cookie.setMaxAge(0);
            response.addCookie(cookie);
            return ResultUtil.resMsgError("没有该用户");
        }
        response.addCookie(cookie);

        return ResultUtil.resObj(one);
    }

}